<!DOCTYPE html>
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:th="http://www.thymeleaf.org"
      xmlns:sec="http://www.thymeleaf.org/thymeleaf-extras-springsecurity3">
<head th:include="public/page_css::page_css ('修改线路')"/>
<body>
<div class="layui-fluid larry-wrapper">
    <div class="layui-row layui-col-space30">
        <div class="layui-col-xs24">
            <section class="panel panel-padding">
                <form action="" class="layui-form layui-form-pane" id="myform">
                    <input type="hidden" th:value="${id}" id="kid" name="id">
                    <div>&nbsp;</div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">路线名称</label>
                        <div class="layui-input-block">
                            <input type="text" class="layui-input" lay-verify="required" name="xlmc" id="xlmc"
                                   placeholder="路线名称"/>
                        </div>
                    </div>
                    <div class="layui-form-item">
                        <label class="layui-form-label">集货库位</label>
                        <div class="layui-input-block">
                            <select name="jhkw" xm-select-radio xm-select="selectId" xm-select-skin="normal"
                                    xm-select-height="40px" ,>
                        </div>
                    </div>
                    <div class="group-button" id="toolbar">
                        <a class="layui-btn layui-btn-sm layui-btn-normal mx-btn" style="text-decoration: none;">
                            <i class="layui-icon">&#x1005;</i> 添加路线
                        </a>
                    </div>
                    <section class="panel panel-padding layui-card-body">
                        <div class="pro">
                            <table id="table13"></table>
                        </div>
                    </section>
                    <div class="layui-form-item" style="float: right;">
                        <div class="layui-input-block">
                            <a class="layui-btn layui-btn-sm layui-btn-normal editLx" style="text-decoration: none;">
                                保存
                            </a>
                        </div>
                    </div>
                </form>
            </section>
        </div>
    </div>
</div>
<!--添加路线-->
<div id="editfunction" style="display: none">
    <div>&nbsp;</div>
    <div class="layui-form-item">
        <label class="layui-form-label">省/市</label>
        <div class="layui-input-block">
            <select name="city" xm-select-radio xm-select="select66" xm-select-skin="normal"
                    xm-select-height="40px"></select>
        </div>
    </div>

</div>
</body>
<div th:include="public/public_js::public_js"></div>
<script th:inline="javascript">
    //省市
    $.ajax({
        url: "/zxsswms/wmsline/getShengInfo",
        type: "GET",
        dataType: "JSON",
        contentType: 'application/json',
        async: false,
        data: JSON.stringify(""),
        success: function (data) {
            layui.formSelects.data('select66', 'local', {
                arr: data,
                height: "auto",
                linkage: true
            });
        },
        error: function () {
            console.log("Error");
        }
    });


    //获取集货库位
    $.ajax({
        url: "/zxsswms/wmsline/hzSearch",
        type: "GET",
        dataType: "JSON",
        contentType: 'application/json',
        async: false,
        data: JSON.stringify(""),
        success: function (data) {
            layui.formSelects.data('selectId', 'local', {
                arr: data,
                height: "auto",
                linkage: true
            });
        },
        error: function () {
            console.log("Error");
        }
    });

    var tabs1;
    layui.use(['myform', 'zzpUtils', 'form', 'jquery'], function () {

        //删除
        window.operateEvent = {
            "click #btn_delete": function (e, value, row, index) {
                var ids = [];
                ids.push(row.shi)
                tabs1.bootstrapTable('remove', {field: 'shi', values: ids});
            }
        }
        var zzpUtils = layui.zzpUtils,
            $ = layui.jquery;

        tabs1 = zzpUtils.table({

            elem: '#table13',
            url: '/wmsline/lingMxSelect',
            isApi: true,
            pagination: false,
            columns: [{
                field: 'sheng',
                title: '省'
            }, {
                field: 'shi',
                title: '市'
            }, {
                field: 'caozuo',
                title: '操作',
                events: operateEvent,
                formatter: operateFormatter
            }],
            queryParams: function () {//查询条件
                return  {
                    id: $('#kid').val()
                };
            }
        });

        function operateFormatter(value, row, index) {
            return [
                '<a class="layui-btn layui-btn-danger layui-btn-xs" id="btn_delete">删除</a>'
            ].join('')
        }
        //预修改查询线路
        zzpUtils.action({
            url:"/wmsline/getUpdateByXl",
            data: JSON.stringify({id:$('#kid').val()}),
            success:function (data) {
                console.log(data);
                zzpUtils.loadJsonDataToForm(data);
                layui.formSelects.value('selectId', [data.jhkw,data.kwzid]);
               // layui.formSelects.value('selectId', [data.kwzid,data.jhkw])
            },error: function (xhr, textStatus) {
                console.log(textStatus);
                console.log(xhr)
            }
        });
        //添加路线
        $(".mx-btn").on('click', function () {
            layer.open({
                type: 1,
                title: "新增路线明细",
                content: $("#editfunction"),
                area: ['70%', '80%'],
                btn: ['保存', '取消'],
                btn1: function (index, layero) {
                    // 取值
                    var title = layui.formSelects.value('select66');
                    var num = parseInt(Math.random() * 100) + 1;
                    var sheng;
                    var shi;
                    if (title == '') {
                        layer.msg("请选择省市！");
                    } else {
                        //获取value
                        resetArray = title[0].value.split("/");
                        //获取值
                        var citys = title[0].name;
                        var arr = citys.split("/");
                        sheng = arr[0];
                        shi = arr[1];
                        var pass = {};
                        pass.sheng = sheng;
                        pass.shi = shi;
//                        tabs1.bootstrapTable('insertRow', {index: num, row: pass});
                        var rows = tabs1.bootstrapTable('getData');
                        console.log('rwos', rows);
                        if (rows.length == 0) {
                            tabs1.bootstrapTable('append', pass);
                        } else {
                            var sb = rows.every(function (item) {
                                var type = true;
                                if (item.sheng == pass.sheng) {
                                    if (item.shi == pass.shi) {
                                        layer.msg("路线已存在，不可重复添加！！！");
                                        type = false;
                                    }
                                }
                                return type;
                            });
                            if (sb) {
                                tabs1.bootstrapTable('append', pass);
                            }
                        }
                    }
                }
                , btn2: function (index, layero) {
                    layer.close(index);
                }
            });
        });
        //提交
        $(".editLx").on('click', function () {
            var rows = tabs1.bootstrapTable('getData');
            var xlmc = $('#xlmc').val();//线路名称
            var kname = layui.formSelects.value('selectId');
            var array = [];
            if (xlmc == '' || kname == ''||rows.length==0) {
                layer.msg("线路名称、集货库位、路线不能为空！");
            } else {
                array = kname[0].value.split("/");
                var pass1 = {};
                pass1.id=$('#kid').val();
                pass1.xlmc = xlmc;
                pass1.jhkw = array[1];
                pass1.kwzid = array[0];
                pass1.list = rows;
                zzpUtils.confirm("是否确定修改", function (index) {
                    zzpUtils.action({
                        url: "/wmsline/baseUpdateByPrimaryKeySelective",
                        data: JSON.stringify(pass1),
                        success: function (data) {
                            setTimeout(function () {
                                if (data.type == 'success') {
                                    top.layer.msg(data.title);
                                    var index = parent.layer.getFrameIndex(window.name); //先得到当前iframe层的索引
                                    parent.layer.close(index); //再执行关闭
                                } else {
                                   // layer.confirm(data.message, {btn: ['确定'], title: data.title, icon: 2});
                                    top.layer.msg(data.message);
                                }
                            }, 500);
                        }, error: function (xhr, textStatus) {
                            console.log(textStatus);
                            console.log(xhr);
                        }
                    })
                })
                return false;
            }
        });


    });
</script>


</html>